package com.silvtar.sync.ui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.BoxLayout;

import com.silvtar.sync.controller.SilverySyncInit;

import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainServicePanel extends JPanel implements ActionListener {

    // 元素
    JLabel          validaterState;
    JButton         validaterButton;

    JLabel          configState;
    JButton         configButton;

    JLabel          silverySyncState;
    JButton         silverySyncButton;

    JLabel          rsyncdState;
    JButton         rsyncdButton;
    private JPanel  panel;
    private JButton reloadButton;

    // 对象
    SilverySyncInit ssInit;
    private JButton refreshButton;

    /**
     * Create the panel.
     */
    public MainServicePanel(SilverySyncInit ssInit) {
        this.ssInit = ssInit;
        setPreferredSize(new Dimension(300, 400));
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        JPanel validaterPanel = new JPanel();
        add(validaterPanel);

        JLabel validaterlabel = new JLabel("验证服务：");
        validaterPanel.add(validaterlabel);

        validaterState = new JLabel("未启动");
        validaterPanel.add(validaterState);

        validaterButton = new JButton("启动");
        validaterButton.addActionListener(this);
        validaterPanel.add(validaterButton);

        JPanel configPanel = new JPanel();
        add(configPanel);

        JLabel configLabel = new JLabel("配置服务：");
        configPanel.add(configLabel);

        configState = new JLabel("未启动");
        configPanel.add(configState);

        configButton = new JButton("启动");
        configButton.addActionListener(this);
        configPanel.add(configButton);

        JPanel silverySyncPanel = new JPanel();
        add(silverySyncPanel);

        JLabel silverySyncLabel = new JLabel("同步服务：");
        silverySyncPanel.add(silverySyncLabel);

        silverySyncState = new JLabel("未启动");
        silverySyncPanel.add(silverySyncState);

        silverySyncButton = new JButton("启动");
        silverySyncButton.addActionListener(this);
        silverySyncPanel.add(silverySyncButton);

        JPanel rsyncdPanel = new JPanel();
        add(rsyncdPanel);

        JLabel rsyncdLabel = new JLabel("Rsync服务：");
        rsyncdPanel.add(rsyncdLabel);

        rsyncdState = new JLabel("未启动");
        rsyncdPanel.add(rsyncdState);

        rsyncdButton = new JButton("启动");
        rsyncdButton.addActionListener(this);
        rsyncdPanel.add(rsyncdButton);

        panel = new JPanel();
        add(panel);

        reloadButton = new JButton("重载主配置文件");
        reloadButton.addActionListener(this);
        panel.add(reloadButton);
        
        refreshButton = new JButton("刷新服务状态");
        refreshButton.addActionListener(this);
        panel.add(refreshButton);

        // 逻辑
        flashServiceState();

    }

    public void flashServiceState() {
        // 验证服务
        if (ssInit.isValidatorOn()) {
            validaterState.setText("已启动");
            validaterButton.setText("重新启动");
        } else {
            validaterState.setText("未启动");
            validaterButton.setText("启动");
        }
        // rsync
        if (ssInit.isRsyncdOn()) {
            rsyncdState.setText("已启动");
            rsyncdButton.setText("重新启动");
        } else {
            rsyncdState.setText("未启动");
            rsyncdButton.setText("启动");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // 验证服务
        if (e.getSource().equals(validaterButton)) {
            ssInit.startValidateDaemon();
        }
        // 配置服务
        if (e.getSource().equals(configButton)) {

        }
        // 同步服务
        if (e.getSource().equals(silverySyncButton)) {

        }
        // rsync服务
        if (e.getSource().equals(rsyncdButton)) {
            ssInit.startRsyncDaemon();
        }
        // 重新载入主配置文件
        if (e.getSource().equals(reloadButton)) {
            ssInit.configSilverySync();
        }
        // 刷新服务状态
        if (e.getSource().equals(refreshButton)) {
            ssInit.refreshDaemonState();
        }
        flashServiceState();
    }

}
